package com.mashibing.juc.c_007;

import java.util.concurrent.Exchanger;

/**
 * @ClassName T03_Exchanger
 * @Description 交互两个线程的值
 * @Author xiongwei.wu
 * @Date 2021/7/12 16:57
 **/
public class T03_Exchanger {
    static Exchanger<String> exchanger = new Exchanger<>();

    public static void main(String[] args) {
        new Thread(() -> {
            String name = "我是T1线程值";
            try {
                name = exchanger.exchange(name);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " " + name);
        }, "T1").start();

        new Thread(() -> {
            String name = "我是T2线程值";
            try {
                name = exchanger.exchange(name);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " " + name);
        }, "T2").start();
    }
}
